cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)

message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")

###########################################################
# Install Qt librarys
###########################################################
set(QT_LIBS
    Qt5Core
    Qt5Designer
    Qt5Gui
    Qt5Network
    Qt5SerialPort
    Qt5Sql
    Qt5Svg
    Qt5Widgets
    # Qt5Xlsx
    Qt5XmlPatterns
    Qt5Xml
    )

if(UNIX)
    list(APPEND QT_LIBS
        Qt5DBus
        Qt5DesignerComponents
        Qt5NetworkAuth
        Qt5XcbQpa
        )
endif()

set(QT_PLUGINS_ICONENGINES qsvgicon)
set(QT_PLUGINS_IMAGEFORMATS
    qgif
    qicns
    qico
    qjpeg
    qsvg
    qtga
    qtiff
    qwbmp
    qwebp)

if(WIN32)
    set(QT_PLUGINS_PLATFORMS qwindows)
else()
    set(QT_PLUGINS_PLATFORMS qxcb)
endif()

set(qt_lib_path                  ${CYG_INSTALL_PREFIX}/lib/qt/lib                 )
set(qt_plugins_iconengines_path  ${CYG_INSTALL_PREFIX}/lib/qt/plugins/iconengines )
set(qt_plugins_imageformats_path ${CYG_INSTALL_PREFIX}/lib/qt/plugins/imageformats)
set(qt_plugins_platforms_path    ${CYG_INSTALL_PREFIX}/lib/qt/plugins/platforms   )

foreach(var ${QT_LIBS})
    if(WIN32)
        message(STATUS "var: ${CMAKE_PREFIX_PATH}/bin/${var}${CMAKE_DEBUG_POSTFIX}.dll")
        install(FILES ${CMAKE_PREFIX_PATH}/bin/${var}${CMAKE_DEBUG_POSTFIX}.dll DESTINATION ${qt_lib_path})
    else()
        install(FILES ${CMAKE_PREFIX_PATH}/lib/lib${var}.so.5.9.3 DESTINATION ${qt_lib_path} RENAME lib${var}.so.5)
    endif()
endforeach()

if(WIN32)
    install(FILES ${CMAKE_PREFIX_PATH}/bin/libEGL${CMAKE_DEBUG_POSTFIX}.dll DESTINATION ${qt_lib_path})
    install(FILES ${CMAKE_PREFIX_PATH}/bin/libGLESV2${CMAKE_DEBUG_POSTFIX}.dll DESTINATION ${qt_lib_path})
    if(MINGW)
        install(FILES ${CMAKE_PREFIX_PATH}/bin/D3Dcompiler_47.dll  DESTINATION ${qt_lib_path})
        install(FILES ${CMAKE_PREFIX_PATH}/bin/libgcc_s_dw2-1.dll  DESTINATION ${qt_lib_path})
        install(FILES ${CMAKE_PREFIX_PATH}/bin/libstdc++-6.dll     DESTINATION ${qt_lib_path})
        install(FILES ${CMAKE_PREFIX_PATH}/bin/libwinpthread-1.dll DESTINATION ${qt_lib_path})
    endif()
endif()

foreach(var ${QT_PLUGINS_ICONENGINES})
    if(WIN32)
        install(FILES ${CMAKE_PREFIX_PATH}/plugins/iconengines/${var}${CMAKE_DEBUG_POSTFIX}.dll DESTINATION ${qt_plugins_iconengines_path})
    else()
        install(FILES ${CMAKE_PREFIX_PATH}/plugins/iconengines/lib${var}.so DESTINATION ${qt_plugins_iconengines_path})
    endif()
endforeach()

foreach(var ${QT_PLUGINS_IMAGEFORMATS})
    if(WIN32)
        install(FILES ${CMAKE_PREFIX_PATH}/plugins/imageformats/${var}${CMAKE_DEBUG_POSTFIX}.dll DESTINATION ${qt_plugins_imageformats_path})
    else()
        install(FILES ${CMAKE_PREFIX_PATH}/plugins/imageformats/lib${var}.so DESTINATION ${qt_plugins_imageformats_path})
    endif()
endforeach()

foreach(var ${QT_PLUGINS_PLATFORMS})
    if(WIN32)
        install(FILES ${CMAKE_PREFIX_PATH}/plugins/platforms/${var}${CMAKE_DEBUG_POSTFIX}.dll DESTINATION ${qt_plugins_platforms_path})
    else()
        install(FILES ${CMAKE_PREFIX_PATH}/plugins/platforms/lib${var}.so DESTINATION ${qt_plugins_platforms_path})
    endif()
endforeach()
